﻿/************************************************************************************
 *      Copyright (C) 2008 supesoft.com,All Rights Reserved						    *
 *      File:																		*
 *				BusinessFacade.cs	                                    			*
 *      Description:																*
 *				 业务逻辑类   													    *
 *      Author:																		*
 *				Lzppcc														        *
 *				Lzppcc@hotmail.com													*
 *				http://www.supesoft.com												*
 *      Finish DateTime:															*
 *				2007年8月6日														*
 *      History:																	*
 ***********************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using BingZi.HY.Core;
using BingZi.HY.Core;
using BingZi.HY.Core;
using System.Data.SqlClient;
using System.Data;
using XCode.Common;
using XCode.DataAccessLayer;
 

namespace BingZi
{
    /// <summary>
    /// 业务逻辑类
    /// </summary>
    public class BusinessFacade
    {

        #region "sys_Applications - Method"


        /// <summary>
        /// 返回sys_ApplicationsTable实体类的ArrayList对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_ApplicationsTable实体类的ArrayList对象</returns>
        public static List<sys_Applications> sys_ApplicationsList(NewLife.QueryParam qp, out int RecordCount)
        {
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "ApplicationID";
            }
            return sys_Applications.FindAll(qp, out RecordCount);
            
        }
        /// <summary>
        /// 根据ID返回 sys_ApplicationsTable实体类 单笔资料
        /// </summary>
        /// <param name="ApplicationID">自动ID 1:为系统管理应用</param>
        /// <returns>返回sys_Applications实体类 ApplicationID为0则无记录</returns>
        public static sys_Applications sys_ApplicationsDisp(int ApplicationID)
        {
            sys_Applications fam = new sys_Applications();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_Applications._.ApplicationID, ApplicationID, NewLife.SearchType.Equal));
            //qp.Where = " Where sys_Applications.ApplicationID = " + ApplicationID;
            int RecordCount = 0;
            List<sys_Applications> lst = sys_Applications.FindAll(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }
        #endregion

        #region "sys_Event - Method"


        /// <summary>
        /// 返回sys_EventTable实体类的ArrayList对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_Event实体类的列表对象</returns>
        public static List<sys_Event> sys_EventList(NewLife.QueryParam qp, out int RecordCount)
        {

            if (qp.Orderfld == null)
            {
                qp.Orderfld = "EventID";
            }
            else if (qp.Orderfld != "EventID")
            {
                qp.Orderfld += ",EventID";
            }
            return sys_Event.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_EventTable实体类 单笔资料
        /// </summary>
        /// <param name="EventID">事件ID号</param>
        /// <returns>返回sys_Event实体类 EventID为0则无记录</returns>
        public static sys_Event sys_EventDisp(int EventID)
        {
            return sys_Event.FindByKeyForEdit(EventID);
        }

        /// <summary>
        /// 清空表sys_Event中所有数据
        /// </summary>
        public static void sys_EventClearData()
        {
           List<sys_Event> list =  sys_Event.FindAll();
           foreach (sys_Event li in list)
           {
               li.Delete();
           }
        }
        #endregion

        #region "sys_Field - Method"
 

        /// <summary>
        /// 返回sys_Field实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_Field实体类的列表对象</returns>
        public static List<sys_Field> sys_FieldList(NewLife.QueryParam qp, out int RecordCount)
        {
 
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "FieldID";
            }
            return sys_Field.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_Field实体类 单笔资料
        /// </summary>
        /// <param name="FieldID">应用字段ID号</param>
        /// <returns>返回sys_Field实体类 FieldID为0则无记录</returns>
        public static sys_Field sys_FieldDisp(int FieldID)
        {
            return sys_Field.FindByKeyForEdit(FieldID);
        }

        /// <summary>
        /// 检测是否违反sys_Field表的PK值
        /// </summary>
        /// <param name="fam">sys_FieldTable类</param>
        /// <param name="pt">PopedomType类型，只对New,Edit有效</param>
        /// <returns></returns>
        public static bool sys_FieldCheckPK(sys_Field fam, PopedomType pt)
        {
            fam.F_Key = Common.inSQL(fam.F_Key);
            NewLife.QueryParam qp = new NewLife.QueryParam();
            if (pt == PopedomType.New)
            {
                qp.Where.Add(new NewLife.SearchParam(sys_Field._.F_Key, fam.F_Key, NewLife.SearchType.Equal));
                //qp.Where = string.Format(" Where F_Key='{0}'", fam.F_Key);
            }
            else if (pt == PopedomType.Edit)
            {
                qp.Where.Add(new NewLife.SearchParam(sys_Field._.F_Key, fam.F_Key, NewLife.SearchType.Equal));
                qp.Where.Add(new NewLife.SearchParam(sys_Field._.FieldID, fam.FieldID, NewLife.SearchType.Ne));
               // qp.Where = string.Format(" Where F_Key='{0}' and FieldID<>{1} ", fam.F_Key, fam.FieldID);
            }
            int RecordCount = 0;
           RecordCount = sys_Field.FindCount(qp);
            //sys_FieldList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region "sys_FieldValue - Method"


        /// <summary>
        /// 返回sys_FieldValue实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_FieldValue实体类的List对象</returns>
        public static List<sys_FieldValue> sys_FieldValueList(NewLife.QueryParam qp, out int RecordCount)
        {

            qp.Orderfld = "V_ShowOrder";
            qp.OrderType = 0;
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "ValueID";
            }
            return sys_FieldValue.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_FieldValue实体类 单笔资料
        /// </summary>
        /// <param name="ValueID">索引ID号</param>
        /// <returns>返回sys_FieldValue实体类 ValueID为0则无记录</returns>
        public static sys_FieldValue sys_FieldValueDisp(int ValueID)
        {
            return sys_FieldValue.FindByKeyForEdit(ValueID);
        }

        /// <summary>
        /// 根据Key,获取相关应用字段列表
        /// </summary>
        /// <param name="Key">应用字段key</param>
        /// <returns>列表</returns>
        public static List<sys_FieldValue> sys_FieldValueFromKey(string Key)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_FieldValue._.V_F_Key, Common.inSQL(Key), NewLife.SearchType.Equal));
            //qp.Where = " Where V_F_Key='" + Common.inSQL(Key) + "' ";
            qp.Orderfld = " V_ShowOrder ";
            qp.OrderType = 0;
            int rInt = 0;
            return sys_FieldValue.FindAll(qp, out rInt);
        }

        /// <summary>
        /// 根据key和code获得应用字段值
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="code">code</param>
        /// <returns>sys_FieldValue实体类</returns>
        public static sys_FieldValue sys_FieldValueFromKey(string key, string code)
        {
            sys_FieldValue fam = new sys_FieldValue();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_FieldValue._.V_F_Key, Common.inSQL(key), NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_FieldValue._.V_Code, Common.inSQL(code), NewLife.SearchType.Equal));
           // qp.Where = string.Format(" Where sys_FieldValue.V_F_Key = '{0}' and sys_FieldValue.V_Code='{1}' ", Common.inSQL(key), Common.inSQL(code));
            int RecordCount = 0;
            List<sys_FieldValue> lst = sys_FieldValue.FindAll(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        #endregion

        #region "sys_Group - Method"
 

        /// <summary>
        /// 返回sys_Group实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_Group实体类的List对象</returns>
        public static List<sys_Group> sys_GroupList(NewLife.QueryParam qp, out int RecordCount)
        {
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "GroupID";
            }
            return sys_Group.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_GroupTable实体类 单笔资料
        /// </summary>
        /// <param name="GroupID">分类ID号</param>
        /// <returns>返回sys_GroupTable实体类 GroupID为0则无记录</returns>
        public static sys_Group sys_GroupDisp(int GroupID)
        {
            return sys_Group.FindByKeyForEdit(GroupID);
        }

        /// <summary>
        /// 获取部门标题路径
        /// </summary>
        /// <param name="GroupID">部门ID</param>
        /// <returns></returns>
        public static string GetGroupTitle(int GroupID)
        {
            int TotalRecord = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            //qp.Where = " where GroupID =  " + GroupID;
            qp.Where.Add(new NewLife.SearchParam(sys_Group._.GroupID, GroupID, NewLife.SearchType.Equal));
            qp.PageIndex = 1;
            qp.PageSize = 1;
            List<sys_Group> lst = sys_GroupList(qp, out TotalRecord);
            if (TotalRecord == 1)
            {
                foreach (sys_Group x in lst)
                {
                    return GetGroupTitle(x.G_ParentID) + ">" + string.Format("<a href='GroupList.aspx?GroupID={0}'>{1}</a>", x.GroupID, x.G_CName);
                }
            }
            return "";
        }

        /// <summary>
        /// 获取当前分类及子分类列表以,号分开
        /// </summary>
        /// <param name="GroupID">部门ID</param>
        /// <returns></returns>
        public static string GetGroupCatID(int GroupID)
        {
            string All_ID = GroupID.ToString();
            int TotalRecord = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_Group._.G_ParentID, GroupID, NewLife.SearchType.Equal));
            //qp.Where = " where G_ParentID =  " + GroupID;
            List<sys_Group> lst = sys_GroupList(qp, out TotalRecord);
            if (TotalRecord > 0)
            {

                foreach (sys_Group x in lst)
                {
                    All_ID = string.Format("{0},{1}", All_ID, GetGroupCatID(x.GroupID));
                    //All_ID + "," + GetGroupCatID(x.GroupID);

                }
            }
            return All_ID;
        }

        /// <summary>
        /// 排序分类ID子分类
        /// </summary>
        /// <param name="GroupID">分类ID</param>
        public static void sys_Group_By(int GroupID)
        {
            int RecordCount = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Orderfld = "G_Level,G_ShowOrder";
            qp.OrderType = 0;
            qp.Where.Add(new NewLife.SearchParam(sys_Group._.G_ParentID, GroupID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_Group._.G_Delete, 0, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where G_ParentID ={0} and G_Delete=0 ", GroupID);
            List<sys_Group> lst = sys_GroupList(qp, out RecordCount);
            RecordCount = 1;
            foreach (sys_Group var in lst)
            {
                var.G_ShowOrder = RecordCount;
                var.Update();
                //Update_Table_Fileds("sys_Group", string.Format("G_ShowOrder={0}", RecordCount), string.Format("GroupID={0}", var.GroupID));
                RecordCount++;
            }
        }
        #endregion

        #region "sys_Module - Method"


        /// <summary>
        /// 返回sys_Module实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_Module实体类的List对象</returns>
        public static List<sys_Module> sys_ModuleList(NewLife.QueryParam qp, out int RecordCount)
        {

            if (qp.Orderfld == null)
            {
                qp.Orderfld = "ModuleID";
            }
            return sys_Module.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_Module实体类 单笔资料
        /// </summary>
        /// <param name="ModuleID">功能模块ID号</param>
        /// <returns>返回sys_Module实体类 ModuleID为0则无记录</returns>
        public static sys_Module sys_ModuleDisp(int ModuleID)
        {
            return sys_Module.FindByKeyForEdit(ModuleID);
        }

        /// <summary>
        /// 根据M_ApplicationID和M_PageCode返回 sys_Module实体类 单笔资料
        /// </summary>
        /// <param name="M_ApplicationID">功能模块ID号</param>
        /// <param name="M_PageCode">M_PageCode</param>
        /// <returns>返回sys_Module实体类 ModuleID为0则无记录</returns>
        public static sys_Module sys_ModuleDisp(int M_ApplicationID, string M_PageCode)
        {
            sys_Module fam = new sys_Module();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_ApplicationID, M_ApplicationID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_PageCode, Common.inSQL(M_PageCode), NewLife.SearchType.Equal));
            //qp.Where = string.Format(" Where sys_Module.M_ApplicationID = {0} and M_PageCode = '{1}'", M_ApplicationID, Common.inSQL(M_PageCode));
            int RecordCount = 0;
            List<sys_Module> lst = sys_ModuleList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 获取模块标题路径
        /// </summary>
        /// <param name="ModuleID">模块ID</param>
        /// <returns></returns>
        public static string GetModuleTitle(int ModuleID)
        {
            int TotalRecord = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.ModuleID, ModuleID, NewLife.SearchType.Equal));
            //qp.Where = " where ModuleID =  " + ModuleID;
            qp.PageIndex = 1;
            qp.PageSize = 1;
            List<sys_Module> lst = sys_ModuleList(qp, out TotalRecord);
            if (TotalRecord == 1)
            {
                foreach (sys_Module x in lst)
                {

                    return GetModuleTitle(x.M_ParentID) + ">" + string.Format("<a href='modulemanager.aspx?S_ID={0}&ModuleID={1}'>{2}</a>", x.M_ApplicationID, x.ModuleID, x.M_CName);
                    //Title = "<a href=\"/Home.aspx?ProgCode=F06M02&C_ID=" + x.C_ID.ToString() + "\">" + x.C_Name + "</a> &raquo; " + Title;
                    //load_M_ProductCatID(x.C_Parent.ToString());
                }
            }
            return "";
        }

        /// <summary>
        /// 检测是否违反sys_Module表的PK值
        /// </summary>
        /// <param name="M_ApplicationID">应用ID</param>
        /// <param name="M_PageCode">PageCode</param>
        /// <param name="Not_IS_ModuleID">ModuleID不等于此ID,当为0时不进行检测</param>
        /// <returns>true存在,false否存在</returns>
        public static bool sys_Module_CheckPK(int M_ApplicationID, string M_PageCode, int Not_IS_ModuleID)
        {
            M_PageCode = Common.inSQL(M_PageCode);
            int RecordCount = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            if (Not_IS_ModuleID == 0)
            {
                qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_ApplicationID, M_ApplicationID, NewLife.SearchType.Equal));
                qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_PageCode, M_PageCode, NewLife.SearchType.Equal));
            }
            else
            {
                qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_ApplicationID, M_ApplicationID, NewLife.SearchType.Equal));
                qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_PageCode, M_PageCode, NewLife.SearchType.Equal));
                qp.Where.Add(new NewLife.SearchParam(sys_Module._.ModuleID, Not_IS_ModuleID, NewLife.SearchType.Ne));
            }

            //qp.Where = Not_IS_ModuleID == 0 ? string.Format("Where M_ApplicationID={0} and M_PageCode='{1}'", M_ApplicationID, M_PageCode) : string.Format("Where M_ApplicationID={0} and M_PageCode='{1}' and ModuleID<>{2}", M_ApplicationID, M_PageCode, Not_IS_ModuleID);
           // sys_ModuleList(qp, out RecordCount);
            RecordCount = sys_Module.FindCount(qp);
            if (RecordCount > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 状态判断
        /// </summary>
        /// <param name="ID">状态ID</param>
        /// <returns>否,是</returns>
        public static string GetStatus(int ID)
        {
            if (ID == 0)
                return "否";
            else
                return "是";
        }

        /// <summary>
        /// 根据应用ID获取所有模板列表
        /// </summary>
        /// <param name="applicationid">应用ID</param>
        /// <returns>模块实体类</returns>
        public static List<sys_Module> sys_Module_GetModules(int applicationid)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            //qp.Where = string.Format(" Where M_ApplicationID={0} ", applicationid);
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_ApplicationID, applicationid, NewLife.SearchType.Equal));
            int rInt = 0;
            return sys_Module.FindAll(qp, out rInt);
        }

        /// <summary>
        /// 根据应用ID删除所有模块
        /// </summary>
        /// <param name="applicationid"></param>
        public static void sys_Module_DeleteFormAppID(int applicationid)
        {
            List<sys_Module> lst = sys_Module_GetModules(applicationid);
            foreach (sys_Module var in lst)
            {
                var.Delete();
                //var.DB_Option_Action_ = "Delete";
                //sys_ModuleInsertUpdate(var);

            }
        }

        /// <summary>
        /// 获得模块列表
        /// </summary>
        /// <returns></returns>
        public static ArrayList sys_Module_List()
        {
            ArrayList lst = new ArrayList();

            if (Common.ApplicationID != 0)
            {
                LoadModule(lst, Common.ApplicationID);
            }
            else
            {
                NewLife.QueryParam qp = new NewLife.QueryParam();
                qp.OrderType = 0;
                qp.Orderfld = "A_Order";
                int rint = 0;
                foreach (sys_Applications var in sys_Applications.FindAll(qp, out rint))
                {
                    LoadModule(lst, var.ApplicationID);
                }
            }
            return lst;
        }

        private static void LoadModule(ArrayList lst, int applicationid)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Orderfld = " M_Applicationid,M_OrderLevel ";
            qp.OrderType = 0;
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_Close, 0, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_ParentID, 0, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_ApplicationID, applicationid, NewLife.SearchType.Equal));
            
            //qp.Where = string.Format("Where M_Close=0 and M_ParentID=0 and M_ApplicationID ={0}", applicationid);
            int RecordCount = 0;
            foreach (sys_Module var in sys_Module.FindAll(qp, out RecordCount))
            {
                lst.Add(var);
            }
        }
        #endregion

        #region "sys_RolePermission - Method"


        /// <summary>
        /// 返回sys_RolePermission实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_RolePermission实体类的List对象</returns>
        public static List<sys_RolePermission> sys_RolePermissionList(NewLife.QueryParam qp, out int RecordCount)
        {
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "PermissionID";
            }
            return sys_RolePermission.FindAll(qp, out RecordCount);
        }

        /// <summary>
        /// 根据ID返回 sys_RolePermission实体类 单笔资料
        /// </summary>
        /// <param name="PermissionID">角色应用权限自动ID</param>
        /// <returns>返回sys_RolePermission实体类 PermissionID为0则无记录</returns>
        public static sys_RolePermission sys_RolePermissionDisp(int PermissionID)
        {
            return sys_RolePermission.FindByKeyForEdit(PermissionID);
        }

        /// <summary>
        /// 删除根据角色ID, 应用ID,角色权限记录
        /// </summary>
        /// <param name="RoleID">角色Id</param>
        /// <param name="ApplicationID">应用ID</param>
        public static void sys_RolePermission_Move(int RoleID, int ApplicationID)
        {
            int RecordCount = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_RoleID, RoleID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_ApplicationID, ApplicationID, NewLife.SearchType.Equal));
    
            //qp.Where = string.Format("Where P_RoleID={0} and P_ApplicationID = {1}", RoleID, ApplicationID);
            List<sys_RolePermission> lst = sys_RolePermissionList(qp, out RecordCount);
            foreach (sys_RolePermission var in lst)
            {
                var.Delete();
                //var.DB_Option_Action_ = "Delete";
                //sys_RolePermissionInsertUpdate(var);
            }
        }

        /// <summary>
        /// 删除根据角色ID,角色权限记录
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        public static void sys_RolePermission_Move(int RoleID)
        {
            int RecordCount = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_RoleID, RoleID, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where P_RoleID={0}", RoleID);
            List<sys_RolePermission> lst = sys_RolePermissionList(qp, out RecordCount);
            foreach (sys_RolePermission var in lst)
            {
                var.Delete();
                //var.DB_Option_Action_ = "Delete";
                //sys_RolePermissionInsertUpdate(var);
            }
        }

        /// <summary>
        /// 获取角色应用权限资料
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        /// <param name="ApplicationID">应用ID</param>
        /// <param name="PageCode">PageCode</param>
        /// <returns></returns>
        public static sys_RolePermission sys_RolePermissionDisp(int RoleID, int ApplicationID, string PageCode)
        {
            sys_RolePermission s_Rp = new sys_RolePermission();
            PageCode = Common.inSQL(PageCode);
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_RoleID, RoleID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_ApplicationID, ApplicationID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_PageCode, PageCode, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where P_RoleID= {0} and P_ApplicationID={1} and P_PageCode='{2}'", RoleID, ApplicationID, PageCode);
            int RecordCount = 0;
            List<sys_RolePermission> lst = sys_RolePermissionList(qp, out RecordCount);
            if (lst.Count > 0)
            {
                s_Rp = lst[0];
            }
            return s_Rp;
        }

        /// <summary>
        /// 根据应用ID,获得角色权限列表
        /// </summary>
        /// <param name="applicationid">应用ID</param>
        /// <returns>角色权限实体类</returns>
        public static List<sys_RolePermission> sys_RolePermission_GetList(int applicationid)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_ApplicationID, applicationid, NewLife.SearchType.Equal));
            //qp.Where = string.Format(" Where P_ApplicationID={0} ", applicationid);
            int rInt = 0;
            return sys_RolePermissionList(qp, out rInt);
        }

        /// <summary>
        /// 根据应用ID,删除角色权限记录
        /// </summary>
        /// <param name="applicationid"></param>
        public static void sys_RolePermission_DeleteFromAppid(int applicationid)
        {
            foreach (sys_RolePermission var in sys_RolePermission_GetList(applicationid))
            {
                var.Delete();
                //var.DB_Option_Action_ = "Delete";
                //sys_RolePermissionInsertUpdate(var);
            }
        }

        /// <summary>
        /// 根据应用Id,pagecode,获得角色权限列表
        /// </summary>
        /// <param name="applicationid">应用id</param>
        /// <param name="pagecode">pagecode</param>
        /// <returns>角色权限实体类</returns>
        public static List<sys_RolePermission> sys_RolePermission_GetList(int applicationid, string pagecode)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_ApplicationID, applicationid, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_PageCode, Common.inSQL(pagecode), NewLife.SearchType.Equal));
            
            //qp.Where = string.Format(" Where P_ApplicationID={0} and P_PageCode='{1}' ", applicationid, Common.inSQL(pagecode));
            int rInt = 0;
            return sys_RolePermissionList(qp, out rInt);
        }

        /// <summary>
        /// 根据应用Id,角色ID,获得权限列表
        /// </summary>
        /// <param name="applicationid">应用id</param>
        /// <param name="roleid">角色ID</param>
        /// <returns>角色权限实体类</returns>
        public static List<sys_RolePermission> sys_RolePermission_GetList(int applicationid, int roleid)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_ApplicationID, applicationid, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_RolePermission._.P_RoleID, roleid, NewLife.SearchType.Equal));
            
            //qp.Where = string.Format(" Where P_ApplicationID={0} and P_RoleID={1} ", applicationid, roleid);
            int rInt = 0;
            return sys_RolePermissionList(qp, out rInt);
        }

        /// <summary>
        /// 根据应用Id和pagecode删除角色权限
        /// </summary>
        /// <param name="applicationid">应用id</param>
        /// <param name="pagecode">pagecode</param>
        public static void sys_RolePermission_DeleteForm(int applicationid, string pagecode)
        {
            foreach (sys_RolePermission var in sys_RolePermission_GetList(applicationid, pagecode))
            {
                //var.DB_Option_Action_ = "Delete";
                //sys_RolePermissionInsertUpdate(var);
                UserData.Move_RoleUserPermissionCache(var.P_RoleID);
                var.Delete();
            }
        }

        #endregion

        #region "sys_Roles - Method"
 

        /// <summary>
        /// 返回sys_Roles实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_Roles实体类的List对象</returns>
        public static List<sys_Roles> sys_RolesList(NewLife.QueryParam qp, out int RecordCount)
        {
  
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "RoleID";
            }
            return sys_Roles.FindAll(qp, out RecordCount);
        }

        /// <summary>
        /// 获得当前登陆用户创建角色列表
        /// </summary>
        /// <param name="qp">查询</param>
        /// <param name="RecordCount">总记录</param>
        /// <returns>角色实体类</returns>
        public static List<sys_Roles> sys_RolesListUser(NewLife.QueryParam qp, out int RecordCount)
        {
            if (UserData.GetUserDate.U_Type != 0)
            {
                qp.Where.Add(new NewLife.SearchParam(sys_Roles._.R_UserID, UserData.GetUserDate.UserID, NewLife.SearchType.Equal));
                //qp.Where = string.Format("Where R_UserID ={0}", UserData.GetUserDate.UserID);
            }
 

            if (qp.Orderfld == null)
            {
                qp.Orderfld = "RoleID";
            }
            return sys_Roles.FindAll(qp, out RecordCount);
        }

        /// <summary>
        /// 根据ID返回 sys_Roles实体类 单笔资料
        /// </summary>
        /// <param name="RoleID">角色ID自动ID</param>
        /// <returns>返回sys_Roles实体类 RoleID为0则无记录</returns>
        public static sys_Roles sys_RolesDisp(int RoleID)
        {
            return sys_Roles.FindByKeyForEdit(RoleID);
        }

        /// <summary>
        /// 检测是否存在相同角色名
        /// </summary>
        /// <param name="R_RoleName">角色名</param>
        /// <param name="Not_IS_RoleID">RoleID不等于此ID,当为0时不进行检测</param>
        /// <returns>存在true,不存在false</returns>
        public static bool sys_Roles_PK(string R_RoleName, int Not_IS_RoleID)
        {
            R_RoleName = Common.inSQL(R_RoleName);
            int RecordCount = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            if (Not_IS_RoleID == 0)
            {
                qp.Where.Add(new NewLife.SearchParam(sys_Roles._.R_RoleName, R_RoleName, NewLife.SearchType.Equal));
            }
            else
            {
                qp.Where.Add(new NewLife.SearchParam(sys_Roles._.R_RoleName, R_RoleName, NewLife.SearchType.Equal));
                qp.Where.Add(new NewLife.SearchParam(sys_Roles._.RoleID, Not_IS_RoleID, NewLife.SearchType.Ne));
            }


            //qp.Where = Not_IS_RoleID == 0 ? string.Format("Where R_RoleName='{0}'", R_RoleName) : string.Format("Where R_RoleName='{0}' and RoleID<>{1}", R_RoleName, Not_IS_RoleID);
            //sys_RolesList(qp, out RecordCount);
            RecordCount = sys_Roles.FindCount(qp);
            if (RecordCount > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 检测当前用户是否可以管理当前角色ID
        /// </summary>
        /// <param name="rolesid">角色id</param>
        public static bool sys_Roles_CheckUser(int rolesid)
        {
            if (UserData.GetUserDate.U_Type == 0)
                return true;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_Roles._.R_UserID, Common.Get_UserID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_Roles._.RoleID, rolesid, NewLife.SearchType.Equal));
            //qp.Where = string.Format(" Where RoleID ={0} and R_UserID={1}", rolesid,UserData.GetUserDate.UserID);
            int RecordCount = 0;
            RecordCount = sys_Roles.FindCount(qp);
           // sys_RolesList(qp, out RecordCount);
            if (RecordCount > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 检测当前用户是否可以管理当前角色ID(直接提示)
        /// </summary>
        /// <param name="rolesid">角色id</param>
        public static void sys_Roles_CheckUserVoid(int rolesid)
        { 
            if (!sys_Roles_CheckUser(rolesid))
                EventMessage.MessageBox(2, "禁止访问", string.Format("你无权操作当前角色ID:{0}!",rolesid), Icon_Type.Error, "history.back();", UrlType.JavaScript);
        }

        /// <summary>
        /// 获得当前用户创建角色
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns>角色id</returns>
        public static List<sys_Roles> sys_RolesList(int userid)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_Roles._.R_UserID, userid, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where R_UserID ={0}", userid);
 

            if (qp.Orderfld == null)
            {
                qp.Orderfld = "RoleID";
            }
            int RecordCount = 0;
            return sys_Roles.FindAll(qp, out RecordCount);
        }


        #endregion

        #region "sys_User - Method"
 

        /// <summary>
        /// 返回sys_UserTable实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_UserTable实体类的List对象</returns>
        public static List<sys_UserTable> sys_UserList(NewLife.QueryParam qp, out int RecordCount)
        {

            if (qp.Orderfld == null && qp.Orderfld == "")
            {
                qp.Orderfld = "UserID";
            }
      
            return sys_UserTable.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_UserTable实体类 单笔资料
        /// </summary>
        /// <param name="UserID">用户ID号</param>
        /// <returns>返回sys_UserTable实体类 UserID为0则无记录</returns>
        public static sys_UserTable sys_UserDisp(int UserID)
        {
          return  sys_UserTable.FindByKeyForEdit(UserID);
        }
        /// <summary>
        /// 根据用户编号获取用户名
        /// </summary>
        /// <param name="UserID">用户编号</param>
        /// <returns>用户名</returns>
        public static string sys_UserGetLoginName(int UserID)
        {
            return sys_UserTable.Find(sys_UserTable._.UserID, UserID).U_LoginName;
        }


        /// <summary>
        /// 检测是否违反sys_User表的PK值
        /// </summary>
        /// <param name="fam">sys_UserTable类</param>
        /// <param name="pt">PopedomType类型，只对New,Edit有效</param>
        /// <returns></returns>
        public static bool sys_UserTableCheckPK(sys_UserTable fam, PopedomType pt)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            if (pt == PopedomType.New)
            {
                qp.Where.Add(new NewLife.SearchParam(sys_UserTable._.U_LoginName, fam.U_LoginName, NewLife.SearchType.Equal));
                //qp.Where = string.Format(" Where U_LoginName='{0}'", fam.U_LoginName);
            }
            else if (pt == PopedomType.Edit)
            {
                qp.Where.Add(new NewLife.SearchParam(sys_UserTable._.U_LoginName, fam.U_LoginName, NewLife.SearchType.Equal));
                qp.Where.Add(new NewLife.SearchParam(sys_UserTable._.UserID, fam.UserID, NewLife.SearchType.Ne));
                //qp.Where = string.Format(" Where U_LoginName='{0}' and UserID<>{1} ", fam.U_LoginName, fam.UserID);

            }
            int RecordCount = 0;
            //sys_UserList(qp, out RecordCount);
            RecordCount = sys_UserTable.FindCount(qp);
            if (RecordCount > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据部门ID,获得用户列表
        /// </summary>
        /// <param name="groupid">部门id</param>
        /// <returns>用户实体类列表</returns>
        public static List<sys_UserTable> sys_UserList(int groupid)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_UserTable._.U_GroupID, groupid, NewLife.SearchType.Equal));
            //qp.Where = string.Format(" Where U_GroupID={0} ", groupid);
            int rInt = 0;
            return sys_UserList(qp, out rInt);
        }

        /// <summary>
        /// 获得用户类型字符
        /// </summary>
        /// <param name="typeid">类型值</param>
        /// <returns>类型字符</returns>
        public static string sys_UserType(int typeid)
        {
            if (typeid == 0)
                return "超级用户";
            else if (typeid == 1)
                return "普通用户";
            else if (typeid == 2)
            {
                return "管理员";
            }
            else
                return "未知用户类型";
        }

        /// <summary>
        /// 检测用户是否是管理员
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns>是/否</returns>
        public static bool sys_UserCheckManager(int userid)
        {
            sys_UserTable ut = UserData.Get_sys_UserTable(userid);
            if (ut.UserID != 0)
            {
                if (ut.U_Type == 2)
                    return true;
            }
            return false;
        }


        /// <summary>
        /// 检测当前用户是否为管理员/超级用户(直接提示)
        /// </summary>
        public static void sys_UserCheckManagerVoid()
        {
            if (!sys_UserCheckManager())
            {
                EventMessage.MessageBox(1, "禁止访问", "你不是管理员/超级用户,无权限执行当前操作!", Icon_Type.Error, "history.back();", UrlType.JavaScript);
            }
        }

        /// <summary>
        /// 检测当前用户是否为管理员/超级用户
        /// </summary>
        /// <returns>是/否</returns>
        public static bool sys_UserCheckManager()
        {
            if (UserData.GetUserDate.U_Type != 2 && UserData.GetUserDate.U_Type != 0)
                return false;
            else
                return true;
        }

        /// <summary>
        /// 检测当前登陆用户是否可以管理当前用户id(超级用户可以管理所有用户,管理员只能管理当前自己部门用户)
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns>是/否</returns>
        public static bool sys_UserCheckManagerUser(int userid)
        {
            if (UserData.GetUserDate.U_Type == 0)
                return true;
            else if (UserData.GetUserDate.U_Type == 1)
                return false;
            else if (UserData.GetUserDate.U_Type == 2)
            {
                sys_UserTable su = BusinessFacade.sys_UserDisp(userid); //获得要检测用户资料
                if (UserData.GetUserDate.U_GroupID == 0 || su.U_GroupID==0) //如果当前登陆用户和检测用户部门id 为0
                    return false;
                if (UserData.GetUserDate.U_GroupID == su.U_GroupID) //如果为同一部门id
                    return true;
                else
                    return false;

            }
            else
                return false;
        }

        #endregion

        #region "sys_RoleApplication - Method"
 

        /// <summary>
        /// 返回sys_RoleApplication实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_RoleApplication实体类的List对象</returns>
        public static List<sys_RoleApplication> sys_RoleApplicationList(NewLife.QueryParam qp, out int RecordCount)
        {
        
            if (qp.Orderfld == null&& qp.Orderfld =="")
            {
                qp.Orderfld = "A_RoleID,A_ApplicationID";
            }
            return sys_RoleApplication.FindAll(qp, out RecordCount);
        }


        /// <summary>
        /// 根据ID返回 sys_RoleApplicationTable实体类 单笔资料
        /// </summary>
        /// <param name="A_RoleID"></param>
        /// <param name="A_ApplicationID"></param>
        /// <returns>返回sys_RoleApplicationTable实体类 为0则无记录</returns>

        public static sys_RoleApplication sys_RoleApplicationDisp(int A_RoleID, int A_ApplicationID)
        {
            sys_RoleApplication fam = new sys_RoleApplication();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_RoleApplication._.A_RoleID, A_RoleID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_RoleApplication._.A_ApplicationID, A_ApplicationID, NewLife.SearchType.Equal));
            //qp.Where = " Where sys_RoleApplication.A_RoleID = " + A_RoleID.ToString() + " sys_RoleApplication.A_ApplicationID=" + A_ApplicationID.ToString();
            int RecordCount = 0;
            List<sys_RoleApplication> lst = sys_RoleApplicationList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 根据角色ID,删除角色应用表
        /// </summary>
        /// <param name="RoleID">角色ID</param>
        public static void sys_RoleApplication_Move(int RoleID)
        {
            int Recordcount = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_RoleApplication._.A_RoleID, RoleID, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where A_RoleID = {0}", RoleID);
            qp.OrderType = 0;
            List<sys_RoleApplication> lst = sys_RoleApplicationList(qp, out Recordcount);
            foreach (sys_RoleApplication var in lst)
            {
                var.Delete();
                //var.DB_Option_Action_ = "Delete";
                //sys_RoleApplicationInsertUpdate(var);
            }
        }

        /// <summary>
        /// 根据应用ID,获得角色应用列表
        /// </summary>
        /// <param name="applicationid">应用ID</param>
        /// <returns>角色应用实体类</returns>
        public static List<sys_RoleApplication> sys_RoleApplication_GetList(int applicationid)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_RoleApplication._.A_ApplicationID, applicationid, NewLife.SearchType.Equal));
           // qp.Where = string.Format(" Where A_ApplicationID={0} ", applicationid);
            int rInt = 0;
            return sys_RoleApplicationList(qp, out rInt);
        }

        /// <summary>
        /// 根据应用ID删除角色应用表
        /// </summary>
        /// <param name="applicationid">应用ID</param>
        public static void sys_RoleApplication_DeleteFormAppid(int applicationid)
        {
            foreach (sys_RoleApplication var in sys_RoleApplication_GetList(applicationid))
            {
                //移除用户角色缓存
                UserData.Move_RoleUserPermissionCache(var.A_RoleID);
                //var.DB_Option_Action_ = "Delete";
                //sys_RoleApplicationInsertUpdate(var);
                var.Delete();
            }
        }
        #endregion

        #region "sys_UserRoles - Method"
 

        /// <summary>
        /// 返回sys_UserRoles实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_UserRoles实体类的List对象</returns>
        public static List<sys_UserRoles> sys_UserRolesList(NewLife.QueryParam qp, out int RecordCount)
        {
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "R_UserID,R_RoleID";
            }
            return sys_UserRoles.FindAll(qp, out RecordCount);
        }


        /// <summary>
        /// 根据ID返回 sys_UserRoles实体类 单笔资料
        /// </summary>
        /// <param name="R_UserID"></param>
        /// <param name="R_RoleID"></param>
        /// <returns>返回sys_UserRoles实体类 为0则无记录</returns>
        public static sys_UserRoles sys_UserRolesDisp(int R_UserID, int R_RoleID)
        {
            sys_UserRoles fam = new sys_UserRoles();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_UserRoles._.R_UserID, R_UserID, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_UserRoles._.R_RoleID, R_RoleID, NewLife.SearchType.Equal));
            //qp.Where = " Where sys_UserRoles.R_UserID = " + R_UserID.ToString() + " and sys_UserRoles.R_RoleID=" + R_RoleID.ToString();
            int RecordCount = 0;
            List<sys_UserRoles> lst = sys_UserRolesList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 根据ID返回 sys_UserRoles实体类 集合
        /// </summary>
        /// <param name="R_UserID"></param>
        /// <returns>返回sys_UserRoles实体类 为0则无记录</returns>
        public static List<sys_UserRoles> sys_UserRolesDisp(int R_UserID)
        {
            sys_UserRoles fam = new sys_UserRoles();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_UserRoles._.R_UserID, R_UserID, NewLife.SearchType.Equal));
            //qp.Where = " Where sys_UserRoles.R_UserID = " + R_UserID.ToString();
            int RecordCount = 0;
            return sys_UserRolesList(qp, out RecordCount);
        }

        /// <summary>
        /// 根据角色ID,返回 sys_UserRoles实体类 集合
        /// </summary>
        /// <param name="roleid">角色ID</param>
        /// <returns>返回sys_UserRoles实体类 为0则无记录</returns>
        public static List<sys_UserRoles> sys_UserRolesList(int roleid)
        {
            sys_UserRolesTable fam = new sys_UserRolesTable();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_UserRoles._.R_RoleID, roleid, NewLife.SearchType.Equal));
            //qp.Where = " Where sys_UserRoles.R_RoleID = " + roleid.ToString();
            int RecordCount = 0;
            return sys_UserRolesList(qp, out RecordCount);
        }
        #endregion

        #region "sys_SystemInfo - Method"

        ///// <summary>
        ///// 新增/删除/修改 sys_SystemInfo
        ///// </summary>
        ///// <param name="fam">sys_SystemInfoTable实体类</param>
        ///// <returns>返回0操正常</returns>
        //public static int sys_SystemInfoInsertUpdate(sys_SystemInfo fam)
        //{
            

        //    int rInt = 0;
        //    using (SqlConnection Conn = new SqlConnection(DAL.Create(sys_SystemInfo.Meta.ConnName).ConnStr))
        //    {
        //        SqlCommand cmd = new SqlCommand("sys_SystemInfoInsertUpdateDelete", Conn);
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        //设置参数
        //        cmd.Parameters.Add("@DB_Option_Action_", SqlDbType.NVarChar).Value = fam.DB_Option_Action_; //操作方法 Insert:增加 Update:修改 Delete:删除 Disp:显示单笔记录
        //        cmd.Parameters.Add("@SystemID", SqlDbType.Int).Value = fam.SystemID;  //自动ID
        //        cmd.Parameters.Add("@S_Name", SqlDbType.NVarChar).Value = fam.S_Name;  //系统名称
        //        cmd.Parameters.Add("@S_Version", SqlDbType.NVarChar).Value = fam.S_Version;  //版本号
        //        cmd.Parameters.Add("@S_Licensed", SqlDbType.VarChar).Value = fam.S_Licensed;  //序列号
        //        cmd.Parameters.Add("@S_SystemConfigData", SqlDbType.Image).Value = FrameSystemInfo.Serializable_sys_ConfigDataTable(fam.S_SystemConfigData);  //系统配置信息
        //        Conn.Open();
        //        rInt = Convert.ToInt32(cmd.ExecuteScalar());
        //        cmd.Dispose();
        //        Conn.Dispose();
        //        Conn.Close();
        //    }
        //    return rInt;
        //}


        /// <summary>
        /// 返回sys_SystemInfoTable实体类的ArrayList对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_SystemInfo实体类的列表对象</returns>
        public static List<sys_SystemInfo> sys_SystemInfoList(NewLife.QueryParam qp, out int RecordCount)
        {

            if (qp.Orderfld == null)
            {
                qp.Orderfld = "SystemID";
            }
            return sys_SystemInfo.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_SystemInfoTable实体类 单笔资料
        /// </summary>
        /// <param name="SystemID">自动ID</param>
        /// <returns>返回sys_SystemInfo实体类 SystemID为0则无记录</returns>
        public static sys_SystemInfo sys_SystemInfoDisp(int SystemID)
        {
            sys_SystemInfo fam = new sys_SystemInfo();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_SystemInfo._.SystemID,SystemID,NewLife.SearchType.Equal));
            //qp.Where = " Where sys_SystemInfo.SystemID = " + SystemID;
            int RecordCount = 0;
            List<sys_SystemInfo> lst = sys_SystemInfoList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }
        #endregion

        #region "sys_Online - Method"


        /// <summary>
        /// 返回sys_Online实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_Online实体类的List对象</returns>
        public static List<sys_Online> sys_OnlineList(NewLife.QueryParam qp, out int RecordCount)
        {
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "OnlineID";
            }
            return sys_Online.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_Online实体类 单笔资料
        /// </summary>
        /// <param name="OnlineID">自动ID</param>
        /// <returns>返回sys_Online实体类 OnlineID为0则无记录</returns>
        public static sys_Online sys_OnlineDisp(int OnlineID)
        {
            return sys_Online.FindByKeyForEdit(OnlineID);
        }

        /// <summary>
        /// 根据UserID返回 sys_Online实体类 单笔资料
        /// </summary>
        /// <param name="sessionid">用户sessionID</param>
        /// <returns>返回sys_Online实体类 OnlineID为0则无记录</returns>
        public static sys_Online sys_OnlineDispSessionID(string sessionid)
        {
            sys_Online fam = new sys_Online();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.Where.Add(new NewLife.SearchParam(sys_Online._.O_SessionID, Common.inSQL(sessionid), NewLife.SearchType.Equal));
            //qp.Where = " Where sys_Online.O_SessionID = '" + Common.inSQL(sessionid) + "'";
            int RecordCount = 0;
            List<sys_Online> lst = sys_OnlineList(qp, out RecordCount);
            if (RecordCount > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 根据用户名读取在线用户
        /// </summary>
        /// <param name="O_UserName">用户名</param>
        /// <returns>返回sys_Online实例</returns>
        public static sys_Online sys_OnlineDisp(string O_UserName)
        {
            sys_Online Online = new sys_Online();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_Online._.O_UserName, Common.inSQL(O_UserName), NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where O_UserName='{0}'", Common.inSQL(O_UserName));
            qp.PageIndex = 1;
            qp.PageSize = 1;
            int rInt = 0;
            List<sys_Online> lst = sys_OnlineList(qp, out rInt);
            if (rInt > 0)
            {
                Online =  lst[0];
            }
            return Online;
        }


        /// <summary>
        /// 根据用户名读取在线用户
        /// </summary>
        /// <param name="O_UserName">用户名</param>
        /// <param name="sessionid">用户SessionID</param>
        /// <returns>返回sys_Online实例</returns>
        public static sys_Online sys_OnlineDisp(string O_UserName, string sessionid)
        {
            sys_Online Online = new sys_Online();
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_Online._.O_SessionID, sessionid, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_Online._.O_UserName, Common.inSQL(O_UserName), NewLife.SearchType.Equal));
           // qp.Where = string.Format("Where O_SessionID='{0}' and O_UserName='{1}'",sessionid, Common.inSQL(O_UserName));
            qp.PageIndex = 1;
            qp.PageSize = 1;
            int rInt = 0;
            List<sys_Online> lst = sys_OnlineList(qp, out rInt);
            if (rInt == 1)
            {
                Online = lst[0];
            }
            return Online;
        }

        /// <summary>
        /// 检测用户sessionid是否在线
        /// </summary>
        /// <param name="sessionid">用户sessionid</param>
        /// <returns>true/false</returns>
        public static bool CheckSessionIDOnline(string sessionid)
        {
            if (sys_OnlineDispSessionID(sessionid).OnlineID == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 检测用户名是否在线
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public static bool CheckMemberOnline(string username)
        {
            if (sys_OnlineDisp(username).OnlineID == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 访问方法
        /// </summary>
        /// <param name="sessionid">用户SessionID</param>
        public static void AccessMemberOnline(string sessionid)
        {
            sys_Online online = sys_OnlineDispSessionID(sessionid);
            online.O_LastTime = DateTime.Now;
            online.O_LastUrl = Common.GetScriptUrl;
            online.Update();
            //online.DB_Option_Action_ = "Update";
            //sys_OnlineInsertUpdate(online);
        }

        /// <summary>
        /// 删除在线用户
        /// </summary>
        /// <param name="sessionid">用户SessionID</param>
        public static void RemoveMemberOnline(string sessionid)
        {
            sys_Online online = sys_OnlineDispSessionID(sessionid);
            online.Delete();
            //online.DB_Option_Action_ = "Delete";
            //sys_OnlineInsertUpdate(online);
        }

        /// <summary>
        /// 插入在线用户表
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="sessionid">用户sessionID</param>
        public static void InsertMemberOnline(string username, string sessionid)
        {
            sys_Online online = new sys_Online();
            online.O_Ip = Common.GetIPAddress();
            online.O_LastTime = DateTime.Now;
            online.O_LastUrl = Common.GetScriptUrl;
            online.O_LoginTime = online.O_LastTime;
            online.O_SessionID = sessionid;
            online.O_UserName = username;
            online.Insert();
            //sys_OnlineInsertUpdate(online);
        }


        #endregion

        #region "sys_ModuleExtPermission - Method"
 

        /// <summary>
        /// 返回sys_ModuleExtPermission实体类的List对象
        /// </summary>
        /// <param name="qp">查询类</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>sys_ModuleExtPermission实体类的List对象</returns>
        public static List<sys_ModuleExtPermission> sys_ModuleExtPermissionList(NewLife.QueryParam qp, out int RecordCount)
        {
  
            if (qp.Orderfld == null)
            {
                qp.Orderfld = "ExtPermissionID";
            }
            return sys_ModuleExtPermission.FindAll(qp, out RecordCount);
        }
        /// <summary>
        /// 根据ID返回 sys_ModuleExtPermission实体类 单笔资料
        /// </summary>
        /// <param name="ExtPermissionID">扩展权限ID</param>
        /// <returns>返回sys_ModuleExtPermission实体类 ExtPermissionID为0则无记录</returns>
        public static sys_ModuleExtPermission sys_ModuleExtPermissionDisp(int ExtPermissionID)
        {
            return sys_ModuleExtPermission.FindByKeyForEdit(ExtPermissionID);
        }

        /// <summary>
        /// 根据模块ID,获得模块新增自定义权限值
        /// </summary>
        /// <param name="moduleid">模块ID</param>
        /// <returns>自定义权限值 0:已经达到最大值,无法再增加!</returns>
        public static int sys_ModuleExtPermissionGetLastPermissionValue(int moduleid)
        {
            int recordcount = 0;
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Where.Add(new NewLife.SearchParam(sys_ModuleExtPermission._.ModuleID, moduleid, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where ModuleID={0}", moduleid);
            qp.Orderfld = "PermissionValue";
            qp.OrderType = 1;
            qp.PageSize = int.MaxValue;
            qp.PageIndex = 1;
            List<sys_ModuleExtPermission> lst = sys_ModuleExtPermissionList(qp, out recordcount);
            if (lst.Count >= 21)
                return 0;
            if (lst.Count == 0)
            {
                return 256 * 2;
            }
            else
            {
                return lst[0].PermissionValue * 2;
            }
        }


        #endregion
        
        #region "根据ModuleID获取当前用户拥有权限的子菜单项"
        /// <summary>
        /// 根据ModuleID获取当前用户拥有权限的子菜单项
        /// </summary>
        /// <param name="ModuleID">ModuleID</param>
        /// <returns></returns>
        public static List<sys_Module> GetPermissionModuleSub(int ModuleID)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Orderfld = " M_OrderLevel ";
            qp.OrderType = 0;
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_Close, 0, NewLife.SearchType.Equal));
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_ParentID, ModuleID, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where M_Close=0 and M_ParentID ={0}", ModuleID);
            int RecordCount = 0;
            List<sys_Module> lst = sys_ModuleList(qp, out RecordCount);
            Remove_MenuNoPermission(lst);
            return lst;
        }
        #endregion

        #region "删除模块"
        /// <summary>
        /// 删除模块(包含子模块)
        /// </summary>
        /// <param name="moduleid">模块ID</param>
        public static void DeleteModule(int moduleid)
        {
            foreach (sys_Module var in BingZiMenuTree.GetAllSubModule(moduleid))
            {
                var.Delete();
                //var.DB_Option_Action_ = "Delete";
                //sys_ModuleInsertUpdate(var);
                //删除角色权限表
                sys_RolePermission_DeleteForm(var.M_ApplicationID, var.M_PageCode);
            }
        }
        #endregion

        #region "获得子模块数"
        /// <summary>
        /// 获得子模块数
        /// </summary>
        /// <param name="ModuleID">模块id</param>
        /// <returns>子模块数</returns>
        public static int GetSuBCount(int ModuleID)
        {
            NewLife.QueryParam qp = new NewLife.QueryParam();
            qp.Orderfld = " M_OrderLevel ";
            qp.OrderType = 0;
            qp.Where.Add(new NewLife.SearchParam(sys_Module._.M_ParentID, ModuleID, NewLife.SearchType.Equal));
            //qp.Where = string.Format("Where M_ParentID ={0}", ModuleID);
            int RecordCount = 0;
            List<sys_Module> lst = sys_Module.FindAll(qp, out RecordCount);
            return RecordCount;
        }
        #endregion

        #region "移除用户无权限菜单项"
        /// <summary>
        /// 移除用户无权限菜单项
        /// </summary>
        /// <param name="lst"></param>
        public static void Remove_MenuNoPermission(List<sys_Module> lst)
        {
            int iCount = lst.Count;
            for (int i = 0; i <= iCount; i++)
            {
                int iIndex = 0;
                foreach (sys_Module var in lst)
                {
                    if (!UserData.CheckPageCode(Common.Get_UserID, var.M_ApplicationID, var.M_PageCode))
                    {
                        lst.RemoveAt(iIndex);
                        break;
                    }
                    iIndex++;
                }
            }
        }
        #endregion

 

    }
}
